package wqb;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); // 设置请求编码
        System.out.println("get这是我的第一个Servlet");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); // 设置请求编码
        response.setCharacterEncoding("UTF-8"); // 设置响应编码
        response.setContentType("text/html;charset=UTF-8"); // 设置响应内容类型.



        System.out.println("post这是我的第一个Servlet");

        PrintWriter out = response.getWriter();
        out.write("<h1>Hello, Java Web！</h1>");
        out.write("<p>这是我的第一个Servlet程序</p>");

        // 获取浏览器传递的参数（比如访问 http://localhost:8080/项目名/hello?name=张三）
        String username = request.getParameter("username");
        if (username != null) {
            out.write("<p>你好，" + username + "！</p>");
        }
    }
}
